﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CustomerService
{
    public partial class CancelOrder : System.Web.UI.Page
    {
        private SalesManagerEntities _sme;

        protected void Page_Load(object sender, EventArgs e)
        {
            _sme = (SalesManagerEntities)Session["sme"];

            ddCancelReason.DataSource = (from c in _sme.codeCanceledReasons where c.Active select c).ToArray();
            ddCancelReason.DataTextField = "Description";
            ddCancelReason.DataValueField = "CanceledReasonID";
            ddCancelReason.DataBind();
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            int orderID = int.Parse(Request.QueryString["OrderID"]);
            tblOrderHeader oh = _sme.tblOrderHeader.Find(new object[] {orderID});

            _sme.spCancelOrder(orderID, int.Parse(ddCancelReason.SelectedValue));

            // Refresh the OrderHeader from the database
            _sme.Entry<tblOrderHeader>(oh).Reload();
            oh.InventoryTransactions.ToArray();
            oh.Transactions.ToArray();
            
            // Trigger the OrderDetail panel to refresh.            
            if (!Page.ClientScript.IsStartupScriptRegistered("PostBack"))
                Page.ClientScript.RegisterStartupScript(this.GetType(), "PostBack",
                    "<script  language='javascript' type='text/javascript'>" +
                    "opener.__doPostBack('" + Request.QueryString["PanelID"] + "', 'UpdateDetails'); window.close('fred');" +
                    "</script>"
                    );
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            if (!Page.ClientScript.IsStartupScriptRegistered("PostBack"))
                Page.ClientScript.RegisterStartupScript(this.GetType(), "PostBack",
                    "<script  language='javascript' type='text/javascript'>" +
                    "window.close('fred');" +
                    "</script>"
                    );
        }

    }
}